﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using Jungle.Code.Utils;
using Jungle.Code.Core;

using Jungle.Code.CategoryManager;
using Jungle.Code.ProductManager;
using Jungle.Code.BookManager;

namespace Jungle.Code.News.WebControls
{
    public class NewsListUserPart: WebControl
    {
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            int catId = -1;
            string cmd = "";
            int nId = 1;
            string searchText = "";

            string linkStr = Context.Request.RawUrl;
            linkStr = linkStr.Substring(0, linkStr.IndexOf("news.aspx") + 9);


            if (Context.Request["catId"] != null)
            { catId = Convert.ToInt32(Context.Request["catId"]); }
            if (Context.Request["cmd"] != null)
            { cmd = Context.Request["cmd"]; }
            if (Context.Request["searchText"] != null)
            { searchText = Context.Request["searchText"]; }
            if (Context.Request["nId"] != null)
            { nId = Convert.ToInt32(Context.Request["nId"]); }

            
            News newsList = new News();

            #region BooksListTable

            #region JustNews

            if (cmd != "search")
            {
               

                newsList.GetAll();

                foreach (News n in newsList.NewsList)
                {
                    writer.AddAttribute("class", "catalog_item");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);

                    writer.AddAttribute("style", "border:1px; cellpadding:0px; cellspacing:0px;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Table);
                    //TABLE


                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.AddAttribute("class", "books_table_td_image");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    
                    //картинка
                    //image
                    try
                    {
                        string[] ImagesUrls = n.Img.ToString().Split(',');
                        if (ImagesUrls[0] == "no_photo")
                            writer.AddAttribute(HtmlTextWriterAttribute.Src, "/UserFiles/Images/Content/no_photo.jpg");
                        else
                            writer.AddAttribute(HtmlTextWriterAttribute.Src, ImagesUrls[0].Trim());
                    }
                    catch (Exception ee)
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Src, "/UserFiles/Images/Content/no_photo.jpg");
                    }  
                    
                    writer.RenderBeginTag(HtmlTextWriterTag.Img); // img
                    writer.RenderEndTag();// img

                    writer.Write("<br />");

                    writer.AddAttribute("class", "catalog_viev_addition_link");
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "/ru/news.aspx?cmd=details&nId=" + n.IdNews.ToString().Trim() + "&large=1");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.Write("Посмотреть увеличенное");
                    writer.RenderEndTag();

                    writer.RenderEndTag(); // картинка td

                    

                    
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //детали

                    writer.RenderBeginTag(HtmlTextWriterTag.Table); // tsble ifo
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr); //название
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); // td
                    writer.Write("<strong>Заголовок: </strong>" + n.Title.Trim());
                    writer.RenderEndTag(); //td
                    writer.RenderEndTag(); //название

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr); //автор
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                    

                    writer.Write("<strong>Текс новости: </strong>" + n.Body.Trim() + "...");
                    writer.RenderEndTag(); //td
                    writer.RenderEndTag(); //автор                  
                                        

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr); // more
                    writer.AddAttribute("class", "books_table_td_info");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "/ru/news.aspx?cmd=details&nId=" + n.IdNews.ToString().Trim());
                    writer.AddAttribute("class", "books_more_link");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.Write("<strong>Подробнее... </strong>");
                    writer.RenderEndTag();
                    writer.RenderEndTag(); //td
                    writer.RenderEndTag(); // more

                    writer.RenderEndTag(); //table info

                    writer.RenderEndTag(); //детали

                    writer.RenderEndTag();


                    //TABLE
                    writer.RenderEndTag();

                    writer.AddAttribute("class", "clear2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    writer.RenderEndTag();

                    //separator
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "height:10px; margin-top:0px;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);

                    writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
                    writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
                    writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:100%;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Table);
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                    writer.AddAttribute(HtmlTextWriterAttribute.Width, "8px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "/App_Themes/img/separator_left.gif");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();

                    writer.AddAttribute("class", "separator_middle");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("&nbsp;");
                    writer.RenderEndTag();


                    writer.AddAttribute(HtmlTextWriterAttribute.Width, "8px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "/App_Themes/img/separator_right.gif");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();

                    writer.RenderEndTag();
                    writer.RenderEndTag();                 
                    writer.RenderEndTag();
                    //separator end
                }

            }else if(cmd == "search" && searchText != "")
            {

                if (newsList.Search(searchText) > 0)
                {
                    foreach (News n in newsList.NewsList)
                    {
                        n.GetById();
                        writer.AddAttribute("class", "catalog_item");
                        writer.RenderBeginTag(HtmlTextWriterTag.Div);

                        writer.AddAttribute("style", "border:0px; cellpadding:0px; cellspacing:0px;");
                        writer.RenderBeginTag(HtmlTextWriterTag.Table);
                        //TABLE


                        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                        writer.AddAttribute("class", "books_table_td_image");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td); //картинка

                        try
                        {
                            string[] ImagesUrls = n.Img.ToString().Split(',');
                            if (ImagesUrls[0] == "no_photo")
                                writer.AddAttribute(HtmlTextWriterAttribute.Src, "/UserFiles/Images/Content/no_photo.jpg");
                            else
                                writer.AddAttribute(HtmlTextWriterAttribute.Src, ImagesUrls[0].Trim());
                        }
                        catch (Exception ee)
                        {
                            writer.AddAttribute(HtmlTextWriterAttribute.Src, "/UserFiles/Images/Content/no_photo.jpg");
                        }  


                        writer.RenderBeginTag(HtmlTextWriterTag.Img); // img
                        writer.RenderEndTag();// img

                        writer.AddAttribute("class", "catalog_viev_addition_link");
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, "/ru/news.aspx?cmd=details&nId=" + n.IdNews.ToString().Trim() + "&large=1");
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.Write("Посмотреть увеличенное");
                        writer.RenderEndTag();
                        
                        writer.RenderEndTag(); // картинка

                        writer.AddAttribute("class", "books_table_td_info");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td); //детали

                        writer.RenderBeginTag(HtmlTextWriterTag.Table); // tsble ifo
                        writer.RenderBeginTag(HtmlTextWriterTag.Tr); //название
                        writer.AddAttribute("class", "books_table_td_info");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td); // td
                        writer.Write("<strong>Заголовок: </strong>" + n.Title.Trim());
                        writer.RenderEndTag(); //td
                        writer.RenderEndTag(); //название

                        writer.RenderBeginTag(HtmlTextWriterTag.Tr); //автор
                        writer.AddAttribute("class", "books_table_td_info");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td); //td


                        writer.Write("<strong>Текс новости: </strong>" + n.Body.Trim() + "...");
                        writer.RenderEndTag(); //td
                        writer.RenderEndTag(); //автор                  


                        writer.RenderBeginTag(HtmlTextWriterTag.Tr); // more
                        writer.AddAttribute("class", "books_table_td_info");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, "/ru/news.aspx?cmd=details&nId=" + n.IdNews.ToString().Trim());
                        writer.AddAttribute("class", "books_more_link");
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.Write("<strong>Подробнее... </strong>");
                        writer.RenderEndTag();
                        writer.RenderEndTag(); //td
                        writer.RenderEndTag(); // more

                        writer.RenderEndTag(); //table info

                        writer.RenderEndTag(); //детали

                        writer.RenderEndTag();


                        //TABLE
                        writer.RenderEndTag();

                        writer.AddAttribute("class", "clear2");
                        writer.RenderBeginTag(HtmlTextWriterTag.Div);
                        writer.RenderEndTag();


                        //separator
                        writer.AddAttribute(HtmlTextWriterAttribute.Style, "height:10px; margin-top:0px;");
                        writer.RenderBeginTag(HtmlTextWriterTag.Div);

                        writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
                        writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
                        writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:100%;");
                        writer.RenderBeginTag(HtmlTextWriterTag.Table);
                        writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                        writer.AddAttribute(HtmlTextWriterAttribute.Width, "8px");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.AddAttribute(HtmlTextWriterAttribute.Src, "/App_Themes/img/separator_left.gif");
                        writer.RenderBeginTag(HtmlTextWriterTag.Img);
                        writer.RenderEndTag();
                        writer.RenderEndTag();

                        writer.AddAttribute("class", "separator_middle");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.Write("&nbsp;");
                        writer.RenderEndTag();


                        writer.AddAttribute(HtmlTextWriterAttribute.Width, "8px");
                        writer.RenderBeginTag(HtmlTextWriterTag.Td);
                        writer.AddAttribute(HtmlTextWriterAttribute.Src, "/App_Themes/img/separator_right.gif");
                        writer.RenderBeginTag(HtmlTextWriterTag.Img);
                        writer.RenderEndTag();
                        writer.RenderEndTag();

                        writer.RenderEndTag();
                        writer.RenderEndTag();
                        writer.RenderEndTag();
                        //separator end
                    }
                }
                else 
                {
                    writer.Write("По вашему запросу ничего не найдено!");
                    writer.AddAttribute("class", "clear2");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    writer.RenderEndTag();

                    writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
                    writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
                    writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
                    writer.RenderBeginTag(HtmlTextWriterTag.Table);
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                    writer.AddAttribute(HtmlTextWriterAttribute.Width, "8px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "/App_Themes/img/separator_left.gif");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();

                    writer.AddAttribute("class", "separator_middle");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write("&nbsp;");
                    writer.RenderEndTag();


                    writer.AddAttribute(HtmlTextWriterAttribute.Width, "8px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "/App_Themes/img/separator_right.gif");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();

                    writer.RenderEndTag();
                }
            }

            #endregion 

            #endregion

            base.RenderContents(writer);
        }
        //--------------------------------------------------------------------
    }
}
